//
//  MJVisitorLoginView.swift
//  新浪微博
//
//  Created by MaJi on 16/4/8.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit
import SnapKit
@objc protocol  MJVisitorLoginViewDelegate:NSObjectProtocol{
    //声明方法
    //登录的协议方法
    //optional 协议也是默认不添加 @objc 但是要使用 optional  就必须添加
//    声明登录的方法
    optional func userWillLogin()
//    声明注册的方法
    optional func userWillResign()
}




class MJVisitorLoginView: UIView {
//创建代理对象
    //swift 中属性默认的修饰 都是strong  注意!!!
    weak var visitorDelegate:MJVisitorLoginViewDelegate?
//
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
//        startAnimation()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func startAnimation() {
        //基本动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //基本设置
        anim.repeatCount = MAXFLOAT
        //设置动画时间
        anim.duration = 15
        //
        anim.toValue = 2 * M_PI
        // 动画执行结束 或者 当前处于非活跃状态下 动画不移除图层
        anim.removedOnCompletion = false
        //添加到 圈圈的图层上
        circleIcon.layer.addAnimation(anim, forKey: nil)
    }
    //    //对外提供设置界面的方法 (为了更改文字,跟图片,否则长的都一样)
    //参数的默认值 这个参数可以不传
    //MARK: 设置UI界面
    func setVisitorInfo(tipText:String, imageName: String,isHome: Bool = false){
        if isHome {
//            开启动画
            startAnimation()
        }else{
            
//            隐藏大房子
            largeHouse.hidden = true
//            将circleIcon移到最顶层
            bringSubviewToFront(circleIcon)
        }
        tipLabel.text = tipText
        circleIcon.image = UIImage(named: imageName)
    }
    private func setupUI() {
//        1.添加子控件
        addSubview(circleIcon)
        addSubview(backImageView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
//        2.设置子控件的约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            //make 就是 circleIcon 取消了frame布局的对象
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            //为了能够自动换行 需要约束宽度
            make.width.equalTo(230)
            make.centerX.equalTo(circleIcon.snp_centerX)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            //约束宽度
            make.width.equalTo(100)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon)
        }
        
        //3.设置背景色
        let value: CGFloat = 237 / 255.0
        backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        
//        4.添加按钮的点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
//    实现按钮的点击事件
    
    @objc private func loginBtnDidClick(){
        //使用代理对象 调用协议方法
        //1.判断代理对象是否不为 nil 2. 判断代理对象是否响应协议方法
        visitorDelegate?.userWillLogin?()
    }
    @objc private func registerBtnDidClick(){
        visitorDelegate?.userWillResign?()
    }
    //把子视图 懒加载出来
    //背景视图
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圈圈  大小和图片的大小一致 没有 origin
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var largeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示文案
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.text = "关注一些人,回到这里喜,关注一些人,回到这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.numberOfLines = 0
        //设置居中
        l.textAlignment = .Center
        //设置 最大显示的宽度
        //        l.preferredMaxLayoutWidth = 230
        //自适应大小
        l.sizeToFit()
        return l
    }()
    
    //登录 注册按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        //设置背景图片
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        return btn
    }()
    
    
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return btn
    }()

}
